package wangjg.android.wifiadmin;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/**
 * 
 * @author wangjg
 *
 */
public class MyWifiAdmin {
	private WifiManager wifiManager;
	
    public MyWifiAdmin(Context context) {
    	wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
    }
    
    public boolean isWifiEnabled(){
    	return wifiManager.isWifiEnabled();
    }
    
    public boolean setWifiEnabled(boolean enabled){
    	return wifiManager.setWifiEnabled(enabled);
    }
    
    public boolean isConnected(){
    	boolean result = false;
    	WifiInfo info = this.getWifiInfo();
    	if(info!=null){
        	int noip = 0;
        	int ipa = info.getIpAddress();
        	if(ipa!=noip){
        		result = true;
        	}
    	}
    	return result;
    }
    
    private WifiInfo getWifiInfo(){
    	WifiInfo info = wifiManager.getConnectionInfo();
    	return info;
    }
    
    public String getSSID(){
    	String ssid = null;
    	WifiInfo info = this.getWifiInfo();
    	if(info!=null){
    		ssid = trimSSID(info.getSSID());
    	}
    	return ssid;
    }
    
    public static String trimSSID(String str){
    	String ssid = str;
    	if(ssid!=null && ssid.startsWith("\"") && ssid.endsWith("\"")){
    		ssid = ssid.substring(1, ssid.length()-1);
    	}
    	return ssid;
    }
    
    public void disconnectWifi(int netId) {
    	wifiManager.disableNetwork(netId); 
    	wifiManager.disconnect(); 
    }
    
    public int queryNetworkId(String SSID){
    	int priority = -1;
    	int networkId = 0;
    	List<WifiConfiguration> list = getConfiguredNetworks();
    	for(WifiConfiguration conf : list){
    		String conf_ssid = trimSSID(conf.SSID);
    		if(conf_ssid.equals(SSID)){
    			if(priority < conf.priority){
    				networkId = conf.networkId;
    				priority = conf.priority;
    			}
    		}
    	}
    	return networkId;
    }
    
    public List<WifiConfiguration> getConfiguredNetworks(){
    	List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    	if(list==null){
    		list = Collections.emptyList();
    	}
    	return list;
    }
    
    public boolean connectTo(String SSID){
    	String[] ssidArray = SSID.split(",");
    	for(String ssid:ssidArray){
        	int networkId = this.queryNetworkId(ssid);
        	if(networkId!=0){
        		boolean success = this.connectTo(networkId);
        		return success;
        	}
    	}
    	return false;
    }
    
    public boolean connectTo(int networkId){
    	boolean result = false;
    	if(networkId!=0){
    		result = wifiManager.enableNetwork(networkId, true);
    	}
        return result;
    }
    
	private static String intToIp(int i){
		return (i&0xFF) + "." + ((i>>8)&0xFF) + "." + ((i>>16)&0xFF) + "." + ((i>>24)&0xFF);
	}
}
